//
//  AppDelegate.swift
//  WangFangWeiBo
//
//  Created by 王方 on 16/7/9.
//  Copyright © 2016年 wf. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
//1.2版本
    var window: UIWindow?
    var observer:NSObjectProtocol?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //一开始的时候就注册通知
      observer =   NSNotificationCenter.defaultCenter().addObserverForName(SwitchRootVCNotification, object: nil, queue: nil) {[weak self] (noti) -> Void in
            //收到了通知后就切换跟视图控制器
            self!.switchRootVC(noti)
        }
        
        
        
        // Override point for customization after application launch.
        //替换主窗口
        window = UIWindow()
        window?.rootViewController = WFMainTBC()
//        window?.rootViewController = WFWelcomeVC()
        window?.makeKeyAndVisible()
        return true
    }

    // MARK:  - 切换跟视图控制器
   @objc private func switchRootVC(noti:NSNotification){
        let obj = noti.object
    if obj is WFOAuthVC {
        window?.rootViewController = WFWelcomeVC()
        print("跳转到欢迎页")
    }else {
        window?.rootViewController = WFMainTBC()
    }
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
       //删除缓存
        WFStatusDAL.clearCacheData()
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    
    // MARK:  - 移除通知
    deinit {
        //NSNotificationCenter.defaultCenter().removeObserver(self)
        NSNotificationCenter.defaultCenter().removeObserver(observer!)
    }
}

